package com.yunding.house.service.impl;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.yunding.house.mapper.WxuserMapper;
import com.yunding.house.pojo.Wxuser;
import com.yunding.house.service.IWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 微信用户关联表 服务实现类
 * </p>
 *
 * @author linyang
 * @since 2019-08-19
 */
@Service
public class WxuserServiceImpl  implements IWxuserService {

    Log log = Log.getLog(WxuserServiceImpl.class);

    @Autowired
    private WxuserMapper wxuserMapper;

    public boolean save(Wxuser newwxuser){

//        log.error("openId:"+openId+" nickName:"+nickName+" unionid:"+unionid+ " headimgurl:"+headimgurl+" country:"+country+" city:"+city+" province:"+province+" sex:"+sex);

        /**
         * 1、判断openId 是否存在
         *    如果存在就update
         *    如果不存在就保存
         */
        Wxuser wxuser = findByOpenId(newwxuser.getOpenId());
        if (wxuser!=null) {
            wxuser.setNickName(newwxuser.getNickName());
            wxuser.setUnionid(newwxuser.getUnionid());
            wxuser.setHeadimgurl(newwxuser.getHeadimgurl());
            wxuser.setCountry(newwxuser.getCountry());
            wxuser.setCity(newwxuser.getCity());
            wxuser.setProvince(newwxuser.getProvince());
            wxuser.setSex(newwxuser.getSex());
            wxuser.setUpdateTime(new Date());
            return wxuserMapper.updateById(wxuser)>=1? true : false;
        }else {
            if (StrKit.notBlank(newwxuser.getOpenId())) {
                Wxuser me = new Wxuser();
                me.setOpenId(newwxuser.getOpenId());
                me.setNickName(newwxuser.getNickName());
                me.setUnionid(newwxuser.getUnionid());
                me.setHeadimgurl(newwxuser.getHeadimgurl());
                me.setCountry(newwxuser.getCountry());
                me.setCity(newwxuser.getCity());
                me.setProvince(newwxuser.getProvince());
                me.setSex(newwxuser.getSex());
                me.setUpdateTime(new Date());
                return wxuserMapper.insertSelective(me);
            }
        }
        return false;
    }


    public Wxuser findByOpenId(String openid){
        System.out.println(openid);
        return wxuserMapper.findByOpenId(openid);
    }

    public boolean update(Wxuser wxuser) {
        return false;
    }

}
